package org.come.Jpanel;

import com.tool.btn.InstanceXBtn;
import com.tool.tcpimg.UIUtils;
import org.come.model.ActiveBase;
import org.come.until.CutButtonImage;
import org.come.until.GoodsListFromServerUntil;

import javax.swing.*;
import java.awt.*;

 

/**
 * 活动model面板
 * 
 * @author HGC<br>
 * @time 2020年3月2日 下午1:12:27<br>
 * @class 类名:InstanceXModelJpanel<br>
 */
public class InstanceXModelJpanel extends JPanel {

    private InstanceXBtn btnGo;

    private ActiveBase activeBase;
    private JLabel labImg, labName, labSite, labNum, labvitalityNum;

    public InstanceXModelJpanel() {
        setPreferredSize(new Dimension(280, 61));
        setOpaque(false);
        setLayout(null);

        getBtnGo();
        getLabImg();
        getLabName();
        getLabNum();
        getLabSite();
        getLabvitalityNum();
    }

    public void showActiveBase(ActiveBase activeBase, int sumReceive) {
        if (activeBase == null) {
            return;
        }
        this.activeBase = activeBase;
        int num = activeBase.getNum();
        StringBuffer buffer = new StringBuffer();
        buffer.append("日次数 ");
        buffer.append(sumReceive);
        buffer.append("/");
        buffer.append(num);
        labNum.setText(buffer.toString());
        buffer.setLength(0);
        int value = activeBase.getValue();
        buffer.append("活跃度 ");
        buffer.append(((sumReceive > num ? num : sumReceive) * value));
        buffer.append("/");
        buffer.append((num * value));
        labvitalityNum.setText(buffer.toString());
        labSite.setText(activeBase.getHead());
        labName.setText(activeBase.getaName());
        labImg.setIcon(GoodsListFromServerUntil.imgpath2(activeBase.getSkin()));
        String guide = activeBase.getGuide();
        if (guide != null&&!"".equals(guide)) {
            if(getStringOneChar(guide, "-") == 3){
                btnGo.setText("前往");
            }else{
                btnGo.setText("自动");
            }
            btnGo.setVisible(true);
        } else {
            btnGo.setVisible(false);
        }
    }
    /**计算某个字符串中包含多少个某个字符串*/
    public int getStringOneChar(String value ,String oldChar){
       String replace = value.replace(oldChar, "");
       return value.length() - replace.length();
    }

    private ImageIcon icon, imgBack;

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        	if (icon == null) {
                icon = CutButtonImage.getImage("inkImg/old/background/S177.png", 345, 2);
            }
            g.drawImage(icon.getImage(), 13, 59, 345, 2, null);
            if (imgBack == null) {
                imgBack = CutButtonImage.getImage("inkImg/old/background/58.png", -1, -1);
            }
            g.drawImage(imgBack.getImage(), 10, 10, 45, 44, null);

    }

    public InstanceXBtn getBtnGo() {
        if (btnGo == null) {
        		btnGo = new InstanceXBtn("skin/8100226/newbutton/B20.png", 1, UIUtils.COLOR_WHITE2, "前往", UIUtils.TEXT_HY16, 2,
                        this);
                btnGo.setBounds(230, 30, 61, 24);
                this.add(btnGo);
        }
        return btnGo;
    }

    public void setBtnGo(InstanceXBtn btnGo) {
        this.btnGo = btnGo;
    }

    public ImageIcon getIcon() {
        return icon;
    }

    public void setIcon(ImageIcon icon) {
        this.icon = icon;
    }

    public ActiveBase getActiveBase() {
        return activeBase;
    }

    public void setActiveBase(ActiveBase activeBase) {
        this.activeBase = activeBase;
    }

    public JLabel getLabImg() {
        if (labImg == null) {
            labImg = new JLabel();
            labImg.setBounds(10, 10, 45, 44);
            this.add(labImg);
        }
        return labImg;
    }

    public void setLabImg(JLabel labImg) {
        this.labImg = labImg;
    }

    public JLabel getLabName() {
        if (labName == null) {
            labName = new JLabel();
            labName.setBounds(57, 9, 100, 20);
            labName.setFont(UIUtils.TEXT_FONT);
            labName.setForeground(Color.white);
            this.add(labName);
        }
        return labName;
    }

    public void setLabName(JLabel labName) {
        this.labName = labName;
    }

    public JLabel getLabSite() {
        if (labSite == null) {
            labSite = new JLabel();
            labSite.setBounds(137, 9, 150, 20);
            labSite.setFont(UIUtils.TEXT_FONT);
            labSite.setForeground(Color.white);
            this.add(labSite);
        }
        return labSite;
    }

    public void setLabSite(JLabel labSite) {
        this.labSite = labSite;
    }

    public JLabel getLabNum() {
        if (labNum == null) {
            labNum = new JLabel();
            labNum.setBounds(57, 34, 100, 20);
            labNum.setFont(UIUtils.TEXT_FONT);
            labNum.setForeground(Color.white);
            this.add(labNum);
        }
        return labNum;
    }

    public void setLabNum(JLabel labNum) {
        this.labNum = labNum;
    }

    public JLabel getLabvitalityNum() {
        if (labvitalityNum == null) {
            labvitalityNum = new JLabel();
            labvitalityNum.setBounds(137, 34, 110, 20);
            labvitalityNum.setFont(UIUtils.TEXT_FONT);
            labvitalityNum.setForeground(Color.white);
            this.add(labvitalityNum);
        }
        return labvitalityNum;
    }

    public void setLabvitalityNum(JLabel labvitalityNum) {
        this.labvitalityNum = labvitalityNum;
    }

    public ImageIcon getImgBack() {

        return imgBack;
    }

    public void setImgBack(ImageIcon imgBack) {
        this.imgBack = imgBack;
    }

}
